<template>
  <div class="w h">
    <div class="category-title a-i-c f-s dialog-page-title">
      <el-button type='text' class="m-r10" icon="ArrowLeft" @click="goBackToPage">返回</el-button>
      <div>
        {{isEdit==this.urlPathCode.TRUE ?'标签编辑':'标签新增' }}
      </div>
    </div>
    <div class="w f-c a-i-c" :style="normalBodyHeight">
      <div style="width:600px">
        <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h"
          :layout="editFromLayout" v-model="editForm">
          <template #colour="{ form, item, layout }">
            <div class="w f-s">
              <el-color-picker v-model="editForm[item.prop]"></el-color-picker>
              <el-input v-model="editForm[item.prop]" disabled class="f-g1"></el-input>
            </div>
          </template>
        </lm-form>
        <div class="lm-w lm-h lm-f-s">
          <div class="lm-w-120 lm-h"></div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="reset" plain>重置</el-button>
          <el-button type="primary" @click="goBackToPage" plain>返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { Tag, type TagInfo } from '@/apis/tag'
import { RequestCode } from '@/server/base'
import { ValidateFieldsError } from 'async-validator'

export default {
  name: 'TagEdit',
  mixins: [frameProperty],
  props: {
    goBackToPage: Function,
    params: [Array, Object]
  } as any,
  data() {
    return {
      editFormRefKey: 'tagModifyRef',

      editForm: {} as {} | TagInfo | any,
      editFromLayout: [
        {
          prop: 'name',
          label: '标签名称',
          type: 'input',
          itemClass: 'column_1',
          message: '请输入2-10个字符',
          required: true,
          clearable: true,
          regex: /^.{0,10}$/
        },
        {
          prop: 'colour',
          type: 'slot',
          required: true,
          clearable: true,
          itemClass: 'column_1',
          label: '标签颜色',
          regex: /^[#][0-9a-zA-Z]{6,8}$/g
        }
      ] as any[]
    }
  },
  computed: {
    modifyInfo() {
      const info = this.params.value as string
      if (!info) {
        return {}
      }
      return this.getParamsByString(info)
    },
    groupId(): string {
      return this.params.groupId + ''
    },
    isEdit():boolean{
      return this.params.isEdit||this.params.isEdit==this.urlPathCode.TRUE;
    }
  },
  methods: {
    getDetailById() {
      this.editForm.labelGroupId = this.groupId
      if (!this.modifyInfo.id) {
        return
      }
      this.editForm = this.modifyInfo
    },
    saveTagInfo() {
      const params = {
        name: this.editForm.name,
        colour: this.editForm.colour,
        labelGroupId: this.groupId
      } as any
      this.modifyInfo.id && (params.id = this.modifyInfo.id)
      const method = this.params.isEdit===this.urlPathCode.TRUE ? Tag.modifyInfo : Tag.appendInfo
      method(params)
        .then((res: RequestCode) => {
          this.showMessage({ type: 'success', message: '操作成功！' })
          this.goBackToPage(true)
        })
        .catch(error => {})
    },
    //验证数据
    submit() {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.saveTagInfo()
      })
    },
    //重置
    reset() {
      this.editForm = {}
    }
  },
  created() {
    this.$nextTick(() => {
      this.getDetailById()
    })
  }
}
</script>

<style scoped>
</style>